package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ActivationCodesMapper;
import com.ruoyi.system.domain.ActivationCodes;
import com.ruoyi.system.service.IActivationCodesService;

/**
 * 激活码增删改查Service业务层处理
 *
 * @author 何志龙
 * @date 2024-09-11
 */
@Service
public class ActivationCodesServiceImpl implements IActivationCodesService {
    @Autowired
    private ActivationCodesMapper activationCodesMapper;

    /**
     * 查询激活码增删改查
     *
     * @param id 激活码增删改查主键
     * @return 激活码增删改查
     */
    @Override
    public ActivationCodes selectActivationCodesById(Long id) {
        return activationCodesMapper.selectActivationCodesById(id);
    }

    @Override
    public ActivationCodes selectActivationCodesByActivationCodes(String activationCodes) {
        return activationCodesMapper.selectActivationCodesByActivationCodes(activationCodes);
    }

    /**
     * 查询激活码增删改查列表
     *
     * @param activationCodes 激活码增删改查
     * @return 激活码增删改查
     */
    @Override
    public List<ActivationCodes> selectActivationCodesList(ActivationCodes activationCodes) {
        return activationCodesMapper.selectActivationCodesList(activationCodes);
    }

    /**
     * 新增激活码增删改查
     *
     * @param activationCodes 激活码增删改查
     * @return 结果
     */
    @Override
    public int insertActivationCodes(ActivationCodes activationCodes) {
        //生成激活码-HBcompanyCode年月productCode数量
        //HB001241000100001
        List<ActivationCodes> items = new ArrayList<>();

        Integer index = 0;


        for (int i = 0; i < activationCodes.getNumbers(); i++) {
            ActivationCodes item = new ActivationCodes();
            BeanUtil.copyProperties(activationCodes, item);
            String code = String.format("HB%s%s%s",
                    activationCodes.getCompany(),
                    DateUtil.format(activationCodes.getGenerationTime(), "yyMM"),
                    activationCodes.getProduct());
            index++;
            System.out.println(index);
            String number = StrUtil.padPre(String.valueOf(index), 5, '0');
            item.setSerialNumber(code + number);

            item.setActivationCode(RandomUtil.randomStringUpper(6));
            System.out.println(code + number);
            items.add(item);
            if ((index % 1000 == 0&& index > 0) || index.equals(Convert.toInt(activationCodes.getNumbers()))) {
                activationCodesMapper.batchInsert(items);
                items.clear();
            }
        }
        return 1;
//        return activationCodesMapper.insertActivationCodes(activationCodes);
    }

    /**
     * 修改激活码增删改查
     *
     * @param activationCodes 激活码增删改查
     * @return 结果
     */
    @Override
    public int updateActivationCodes(ActivationCodes activationCodes) {
        return activationCodesMapper.updateActivationCodes(activationCodes);
    }

    /**
     * 批量删除激活码增删改查
     *
     * @param ids 需要删除的激活码增删改查主键
     * @return 结果
     */
    @Override
    public int deleteActivationCodesByIds(Long[] ids) {
        return activationCodesMapper.deleteActivationCodesByIds(ids);
    }

    /**
     * 删除激活码增删改查信息
     *
     * @param id 激活码增删改查主键
     * @return 结果
     */
    @Override
    public int deleteActivationCodesById(Long id) {
        return activationCodesMapper.deleteActivationCodesById(id);
    }
}
